package com.weijing.pretty_app.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.ActivityMineBinding
import com.weijing.pretty_app.fragment.AlarmFragment
import com.weijing.pretty_app.fragment.HandleFragment
import com.weijing.pretty_app.fragment.HelpFragment
import com.weijing.pretty_app.fragment.PasswordFragment
import com.weijing.pretty_app.fragment.SystemFragment
import com.weijing.pretty_app.fragment.UserFragment
import com.weijing.pretty_app.util.KeyUtil

private const val TAG = "MineActivity"

/**
 * 我的页面
 */
class MineActivity : BaseActivity() {

    private val binding by lazy { ActivityMineBinding.inflate(layoutInflater) }

    private val fragmentList = mutableListOf<Fragment>()
    private var userFragment: UserFragment? = null
    private var systemFragment: SystemFragment? = null
    private var alarmFragment: AlarmFragment? = null
    private var handleFragment: HandleFragment? = null
    private var pwdFragment: PasswordFragment? = null
    private var helpFragment: HelpFragment? = null

    private var llList = mutableListOf<LinearLayout>()

    private var tvList = mutableListOf<AppCompatTextView>()
    override fun getMainView(): View = binding.main

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        llList.add(binding.mineLlUserList)
        llList.add(binding.mineLlSystemSettings)
        llList.add(binding.mineLlAlarmSettings)
        llList.add(binding.mineLlPassword)
        llList.add(binding.mineLlHandle)
        llList.add(binding.mineLlHelp)

        tvList.add(binding.mineTvUserList)
        tvList.add(binding.mineTvSystemSettings)
        tvList.add(binding.mineTvAlarmSettings)
        tvList.add(binding.mineTvPassword)
        tvList.add(binding.mineTvHandle)
        tvList.add(binding.mineTvHelp)
        initClick()

        showFragment()
    }

    private fun initClick() {
        binding.mineIvBack.setOnClickListener { finish() }

        binding.mineIvHome.setOnClickListener {
            startActivity(Intent(this, MenuSelectActivity::class.java))
            finish()
        }

        binding.mineLlUserList.setOnClickListener {
            resetButtonBackground(
                binding.mineLlUserList, binding.mineIvUserList,
                R.mipmap.icon_user_select, binding.mineTvUserList
            )
            binding.mineViewPager.setCurrentItem(0, false)
        }

        binding.mineLlSystemSettings.setOnClickListener {
            resetButtonBackground(
                binding.mineLlSystemSettings, binding.mineIvSystemSettings,
                R.mipmap.icon_setting_select, binding.mineTvSystemSettings
            )
            binding.mineViewPager.setCurrentItem(1, false)
        }

        binding.mineLlAlarmSettings.setOnClickListener {
            resetButtonBackground(
                binding.mineLlAlarmSettings, binding.mineIvAlarmSettings,
                R.mipmap.icon_alarm_select, binding.mineTvAlarmSettings
            )
            binding.mineViewPager.setCurrentItem(2, false)
        }

        binding.mineLlPassword.setOnClickListener {
            setPwdIndex()
        }

        binding.mineLlHandle.setOnClickListener {
            setHandleIndex()
        }

        binding.mineLlHelp.setOnClickListener {
            resetButtonBackground(
                binding.mineLlHelp, binding.mineIvHelp,
                R.mipmap.icon_help_select, binding.mineTvHelp
            )
            binding.mineViewPager.setCurrentItem(5, false)
        }
    }

    private fun setPwdIndex() {
        resetButtonBackground(
            binding.mineLlPassword, binding.mineIvPassword,
            R.mipmap.icon_pwd_select, binding.mineTvPassword
        )
        binding.mineViewPager.setCurrentItem(3, false)
    }

    private fun setHandleIndex() {
        resetButtonBackground(
            binding.mineLlHandle, binding.mineIvHandle,
            R.mipmap.icon_handle_select, binding.mineTvHandle
        )
        binding.mineViewPager.setCurrentItem(4, false)
    }

    private fun showFragment() {
        if (userFragment == null) {
            userFragment = UserFragment()
        }
        if (systemFragment == null) {
            systemFragment = SystemFragment()
        }
        if (alarmFragment == null) {
            alarmFragment = AlarmFragment()
        }
        if (handleFragment == null) {
            handleFragment = HandleFragment()
        }
        if (pwdFragment == null) {
            pwdFragment = PasswordFragment()
        }
        if (helpFragment == null) {
            helpFragment = HelpFragment()
        }
        fragmentList.clear()
        fragmentList.add(userFragment!!)
        fragmentList.add(systemFragment!!)
        fragmentList.add(alarmFragment!!)
        fragmentList.add(pwdFragment!!)
        fragmentList.add(handleFragment!!)
        fragmentList.add(helpFragment!!)
        binding.mineViewPager.offscreenPageLimit = 1
        binding.mineViewPager.adapter = ScreenSlidePagerAdapter(fragmentList, this)
        binding.mineViewPager.isUserInputEnabled = false

        var defaultIndex = 0
        intent.extras?.containsKey(KeyUtil.PWD_KEY)?.let {
            if (it) {
                defaultIndex = intent.extras?.getInt(KeyUtil.PWD_KEY)!!
                setPwdIndex()
            }
        }

        //首页过来就禁止修改密码之外的按钮点击
        intent.extras?.containsKey(KeyUtil.MAIN_TO_PWD)?.let {
            if (it) {
                val isFlag = intent.extras?.getBoolean(KeyUtil.MAIN_TO_PWD)!!
                if (isFlag) {
                    disableAllClicks()
                }
            }
        }


        //handle
        intent.extras?.containsKey(KeyUtil.HANDLE_KEY)?.let {
            if (it) {
                defaultIndex = intent.extras?.getInt(KeyUtil.HANDLE_KEY)!!
                setHandleIndex()
            }
        }

        binding.mineViewPager.setCurrentItem(defaultIndex, false)
    }

    private fun resetButtonBackground(
        ll: LinearLayout, iv: AppCompatImageView, iconSelectId: Int, tv: AppCompatTextView
    ) {

        llList.forEach {
            it.setBackgroundResource(R.drawable.shape_041332_radius4)
        }

        tvList.forEach {
            it.setTextColor(ContextCompat.getColor(this, R.color.color_00C1CD))
        }

        binding.mineIvUserList.setImageResource(R.mipmap.icon_user_unselect)
        binding.mineIvSystemSettings.setImageResource(R.mipmap.icon_setting_unselect)
        binding.mineIvAlarmSettings.setImageResource(R.mipmap.icon_alarm_unselect)
        binding.mineIvPassword.setImageResource(R.mipmap.icon_pwd_unselect)
        binding.mineIvHandle.setImageResource(R.mipmap.icon_handle_unselect)
        binding.mineIvHelp.setImageResource(R.mipmap.icon_help_unselect)

        //设置选中的状态
        ll.setBackgroundResource(R.drawable.shape_mine_select)
        iv.setImageResource(iconSelectId)
        tv.setTextColor(ContextCompat.getColor(this, R.color.black))
    }

    private class ScreenSlidePagerAdapter(val lists: List<Fragment>, fa: FragmentActivity?) :
        FragmentStateAdapter(fa!!) {
        override fun createFragment(position: Int): Fragment {
            return lists[position]
        }

        override fun getItemCount(): Int {
            return lists.size
        }
    }

    // 批量禁止按钮点击
    private fun disableAllClicks() {
        for (ll in llList) {
            // 设置当前 LinearLayout 为不可点击
            ll.isClickable = false
            ll.isFocusable = false
            ll.isFocusableInTouchMode = false

            // 遍历当前 LinearLayout 的所有子视图，设置为不可点击
            for (i in 0 until ll.childCount) {
                val child = ll.getChildAt(i)
                child.isClickable = false
                child.isFocusable = false
                child.isFocusableInTouchMode = false
            }
        }
        binding.mineIvHome.isClickable = false
        binding.mineIvHome.isFocusable = false
        binding.mineIvHome.isFocusableInTouchMode = false
    }
}